作者:偏偏喜欢你_Jerry_207 | 来源:互联网 | 2024-10-17 11:09
1、当月有多少天
1)maven依赖
org.apache.commonscommons-lang3
2)java方法
/**
* 当月有多少天
* @param month 月份,eg:2021-09,2021-09-22
* @return
*/
public static int getTotalDayOfMonth(String month) {Date date = DateUtils.parseDate(month);Calendar calendar = Calendar.getInstance();calendar.setTime(date);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));SimpleDateFormat sdf = new SimpleDateFormat("dd");String lastDay = sdf.format(calendar.getTime());return Integer.parseInt(lastDay);
}
2、获取两日期之间的月份以及每月对应的天数
1)结果示例
2)java代码
/*** 两日期之间的月份与天数* &#64;param dateStart YYYYMMDD* &#64;param dateEnd YYYYMMDD dateEnd需大于dateStart* &#64;return key: YYYYMM,val:天数*/public static Map betweenDay(String dateStart, String dateEnd) {Map dataMap &#61; MapUtil.newHashMap();String firstMonth &#61; getYearAndMonth(dateStart);int firstMonthDay &#61; getDayInt(dateStart);int firstMonthTotalDay &#61; getTotalDayOfMonth(dateStart);// 开始月&#xff0c;剩余多少天int firstMonthBetweenDay &#61; firstMonthTotalDay - firstMonthDay &#43; 1;dataMap.put(firstMonth, firstMonthBetweenDay);// 最后一个月String lastMonth &#61; getYearAndMonth(dateEnd);// 最后一个月&#xff0c;间隔多少天int lastMonthBetweenDay &#61; getDayInt(dateEnd);dataMap.put(lastMonth, lastMonthBetweenDay);String tmpYear &#61; null;String tmpMonth &#61; null;int tmpMonthInt &#61; 0;int tmpYearInt &#61; 0;int monthTotal &#61; 12;int daysNum &#61; 0;for (String tmpDate &#61; firstMonth; tmpDate.compareTo(lastMonth) <0;) {tmpYear &#61; getYear(tmpDate);tmpYearInt &#61; Integer.parseInt(tmpYear);tmpMonth &#61; getMonth(tmpDate);tmpMonthInt &#61; Integer.parseInt(tmpMonth);tmpMonthInt&#43;&#43;;if (tmpMonthInt > monthTotal) {tmpYearInt&#43;&#43;;tmpMonthInt %&#61; monthTotal;}tmpDate &#61; tmpYearInt &#43; formatMonth(tmpMonthInt);if (tmpDate.compareTo(lastMonth) >&#61; 0) {break;}daysNum &#61; getTotalDayOfMonth(tmpDate);dataMap.put(tmpDate, daysNum);}return dataMap;}
3&#xff09;其他依赖
/*** 获取年* &#64;param date YYYYMMDD* &#64;return YYYY*/public static String getYear(String date) {date &#61; StringUtils.val(date).replaceAll("-", "");if (date.length() > 3) {return date.substring(0, 4);}return date;}/*** 获取月* &#64;param date YYYYMMDD* &#64;return MM*/public static String getMonth(String date) {date &#61; StringUtils.val(date).replaceAll("-", "");if (date.length() > 5) {return date.substring(4, 6);}return date;}/*** 获取日* &#64;param date YYYYMMDD* &#64;return MM*/public static String getDay(String date) {date &#61; StringUtils.val(date).replaceAll("-", "");int length &#61; date.length();return date.substring(6, length);}/*** 获取日* &#64;param date YYYYMMDD* &#64;return*/public static int getDayInt(String date) {return Integer.parseInt(getDay(date));}/*** 获取年月* &#64;param date YYYYMMDD* &#64;return YYYYMM*/public static String getYearAndMonth(String date) {return getYear(date) &#43; getMonth(date);}/*** 格式化月* &#64;param month 1* &#64;return 01*/public static String formatMonth(int month) {return String.format("%02d", month);}